/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorDocumentPropertiesPlugin = Class.create(FNISVGEditorPlugin, {
  initialize : function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);
    
    var button = new FNIPushButton({});
    
    button.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/document-properties.png");
    button.domNode.addClassName('fniSVGEditor_DocumentPropertiesPlugin_Button');
    button.addListener('buttonClick', this, this._onButtonClick);
    button.setTooltip(SVGLocale.getText("documentpropertiesPlugin_buttonTooltip"));
    editorInstance.registerToolbarItem('documentProperties', button);
  },
  getName : function() {
    return "documentproperties";
  },
  _onButtonClick : function(event) {
    var dialog = this.getEditorInstance().openDialog({
      contentURL: this.getPluginURL() + '/dialog.html',
      iconURL: this.getPluginURL() + '/gfx/icons/16x16/actions/document-properties.png',
      title: SVGLocale.getText("documentpropertiesPlugin_dialogTitle"),
      customCSS: this.getPluginURL() + '/dialog.css'
    });
    
    var _this = this;
    
    dialog.addListener("onLoad", function (event) {
      var pageSize = _this.getEditorInstance().getViewPort().getPageSize();
      
      var dialogDocument = dialog.getContentElement().ownerDocument;
      var widthInput = dialogDocument.getElementById('documentPropertiesDialogWidthInput');
      widthInput.value = pageSize.width; 
      var heightInput = dialogDocument.getElementById('documentPropertiesDialogHeightInput');
      heightInput.value = pageSize.height;
      var submitElement = dialogDocument.getElementById('documentPropertiesSubmitButton');
      var cancelElement = dialogDocument.getElementById('documentPropertiesCancelButton');
      
      Event.observe(submitElement, "click", function (event) { 
        var width = parseInt(widthInput.value);
        var height = parseInt(heightInput.value);
        
        if ((width>0)&&(height>0)) {
          _this.getEditorInstance().getViewPort().setPageSize(width, height);
          dialog.close();
        } else {
          alert(SVGLocale.getText("documentPropertiesPlugin_widthAndHeightMustBeNumbers"));
        }
      });
      
      Event.observe(cancelElement, "click", function (event) { 
        dialog.close();
      });
      
      dialog.localize();
      dialog.center();
      dialog.setHeight(116);
      dialog.show();
    });
  }
});

document.fire("svgEditorPluginLoaded:documentproperties", {
  pluginClass : FNISVGEditorDocumentPropertiesPlugin
});
